<!-- 装具容量 -->
<template>
  <div class="container">
    <div ref="cabinetCapacity" :style="`width: 100%; height: ${cardHeight}px`"></div>
  </div>
</template>

<script>
import * as echarts from 'echarts'
import { allCabinet } from '@/api/rfid/virtualRepository.js'
import throttle from '@/utils/throttle.js'

export default {
  name: 'cabinetCapacity',
  components: {},
  props: {
    cardHeight: {
      type: Number,
      default: 250,
    },
  },
  data() {
    return {
      cabinet: [],
      myChart: null,
      throttledResize: null,
    }
  },
  computed: {},
  watch: {},
  created() { },
  mounted() {
    this.getAllCabinet()
    this.throttledResize = throttle(this.onWindowResize, 200)
    window.addEventListener('resize', this.throttledResize)
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.throttledResize)
  },
  methods: {
    // 获取装具容量
    getAllCabinet(val) {
      allCabinet(val).then((res) => {
        if (res.data.code == 200) {
          this.cabinet = res.data.data
        } else {
          this.cabinet.length = 0
        }
        this.initOutInEcharts()
      })
    },
    onWindowResize() {
      this.myChart.resize()
    },

    initOutInEcharts() {
      // 判断dom是否存在，要是不存在再进行初始化
      var chartDom = this.$refs.cabinetCapacity
      this.myChart = echarts.init(chartDom)
      if (!this.myChart) return

      var option
      option = {
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow',
          },
        },
        legend: {},
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true,
        },
        xAxis: {
          type: 'category',
          // boundaryGap: [0, 0.01],
          axisLabel: {
            interval: 0, // 强制显示所有标签
            formatter: function (value) {
              // 超出部分显示省略号
              if (value.length > 5) {
                return `${value.slice(0, 5)}...`
              }
              return value
            },
          },
          data: this.cabinet.map((x) => x.deviceName),
        },
        yAxis: {
          type: 'value',
        },
        series: [
          {
            name: '总容量(cm)',
            type: 'bar',
            data: this.cabinet.map((x) => x.allDivWidth),
          },
          {
            name: '已用容量(cm)',
            type: 'bar',
            data: this.cabinet.map((x) => x.boxCount),
          },
        ],
      }
      option && this.myChart.setOption(option)
    },
  },
}
</script>
<style lang="less" scoped></style>
